﻿using System.Windows;
using Microsoft.Extensions.DependencyInjection;
using Serilog;
using SimpleTool.SerialTool.SerialControl;
using SimpleTool.Server;
using SimpleTool.SerialTool;
using SimpleTool.Capacity;
using SimpleTool.ActivityUnit;
using System.Diagnostics;
using System.Text;
using System.Runtime.InteropServices;
using System.Reflection;
using System.Threading.Tasks;

namespace SimpleTool;

/// <summary>
/// Interaction logic for App.////  / </summary>
public partial class App : Application, IDisposable
{
    public readonly RuntimeLog MyLog;

    public IServiceProvider ServiceProvider { get; private set; }

    public App()
    {
        ServiceCollection Service = new();

        Service.AddSingleton<IDirFileServer, DirFileServer>();

        Service.AddSingleton<ISerialServer, SerialConfigServer>();

        Service.AddSingleton<IDataBaseServer, DatabaseServer>();

        Service.AddSingleton<IDenCryptoGraphicServer, DenCryptoGraphicServer>();

        Service.AddSingleton<IActivityDetetorServer, ActivityDetetorServer>();

		Service.AddSingleton<IPowerPlantServer, PowerPlantServer>();

        Service.AddSingleton<MainViewModel>();

        Service.AddSingleton<SerialViewModel>();

        Service.AddSingleton<OverViewModel>();

        Service.AddSingleton<CapacityViewModel>();

        Service.AddSingleton<ActivityViewModel>();

        ServiceProvider = Service.BuildServiceProvider();

        System.Windows.Media.RenderOptions.ProcessRenderMode = System.Windows.Interop.RenderMode.SoftwareOnly;

        PresentationTraceSources.DataBindingSource.Switch.Level = SourceLevels.Warning;

		Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);

        MyLog = new();
    }

    protected override void OnExit(ExitEventArgs e)
    {
        Dispose();
        base.OnExit(e);
    }

    /// <summary>
    /// avoid duplication release , add to the queue
    /// </summary>
    public void Dispose()
    {
        (ServiceProvider as IDisposable)?.Dispose();
        GC.SuppressFinalize(this);
        Log.Logger.Print(RuntimeLog.LogType.Warning, $"App {MethodBase.GetCurrentMethod()?.Name} Onexit");
    }
}

